package in.falkeninc.umt_v0_9_8_1;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.TimeZone;

import android.content.Context;
import android.net.ParseException;
import android.util.Log;

public class AFW_Time {

	public enum Day {
	    MONDAY, TUESDAY, WEDNESDAY,THURSDAY, FRIDAY, SATURDAY, SUNDAY; 
	}
	
	/**
	 * @param context
	 * @param timeToFormat
	 * @return Returns the passed time as a formatted String
	 * @throws java.text.ParseException
	 */
	public String formatDateTime(Context context, String timeToFormat) throws java.text.ParseException {
	
	    String finalDateTime = "";          

	    SimpleDateFormat iso8601Format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US);
	    
	    Date date = null;
	    if (timeToFormat != null) {
	        try {
	            date = iso8601Format.parse(timeToFormat);
	        } catch (ParseException e) {
	            date = null;
	        }
	
	        if (date != null) {
	            long when = date.getTime();
	            int flags = 0;
	            flags |= android.text.format.DateUtils.FORMAT_SHOW_TIME;
	            flags |= android.text.format.DateUtils.FORMAT_SHOW_DATE;
	            flags |= android.text.format.DateUtils.FORMAT_ABBREV_MONTH;
	            flags |= android.text.format.DateUtils.FORMAT_SHOW_YEAR;
	
	            finalDateTime = android.text.format.DateUtils.formatDateTime(context,
	            when + TimeZone.getDefault().getOffset(when), flags);               
	        }
	    }
	    return finalDateTime;
	}
	
	/**
	 * @param year
	 * @param month
	 * @param day
	 * @param hour
	 * @param min
	 * @param sec
	 * @return Returns the passed time as a Long millisecond time stamp.
	 */
	public long convertDateIntegersToMillis(int year,int month, int day, int hour, int min, int sec) {
		GregorianCalendar c = new GregorianCalendar(year,month-1,day,hour,min,sec);
		long ms = c.getTimeInMillis();
		return ms;
	}

	/**
	 * @param year
	 * @param month
	 * @param day
	 * @param hour
	 * @param min
	 * @param sec
	 * @return Returns the passed time as a readable String.
	 */
	public String convertDateIntegersToLocalTime(int year,int month, int day, int hour, int min, int sec) {
		GregorianCalendar c = new GregorianCalendar(year,month-1,day,hour,min,sec);
		Date tz = c.getTime();
		return tz.toString();
	}

	/**
	 * @param year
	 * @param month
	 * @param day
	 * @param hour
	 * @param min
	 * @param sec
	 * @return Returns the passed time as a Long millisecond time stamp.
	 */
	public long convertDateStringsToMillis(String year,String month, String day, String hour, String min, String sec) {
		GregorianCalendar c = new GregorianCalendar(Integer.parseInt(year),
													Integer.parseInt(month),
													Integer.parseInt(day),
													Integer.parseInt(hour),
													Integer.parseInt(min),
													Integer.parseInt(sec));
		long ms = c.getTimeInMillis();
		return ms;
	}

	/**
	 * @return Returns the current time in a millisecond timestamp as a Long. 
	 */
	public long getCurrentTimeMillis() {
		
		Calendar r = GregorianCalendar.getInstance();
		long t = r.getTimeInMillis();
		return t;
		
	}
	
	/**
	 * @return Returns the complete current time as a readable String.
	 */
	public String getCurrentTimeString() {
		
		Calendar r = GregorianCalendar.getInstance();
		Date t = r.getTime();
		return t.toString();
		
	}

/*	@SuppressWarnings("deprecation")
	public String convertMillisecondsToDateString(long ms) {

		Date d = new Date();
		d.setTime(ms);
		return d.toLocaleString();
		
	}*/

	public void testformat(){
		
	   	String tst = "";
	   	try {
			tst = this.formatDateTime(null, "2013-12-24 13:13:13");
		} catch (java.text.ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	   	Log.d("testformat: ", tst);

	}
	
   	// get the current time to fill in the parts we have to assume.
   	// the user only inputs hour and minute
   	/*   	Calendar r = GregorianCalendar.getInstance();

   	int sec = r.get(Calendar.SECOND);
   	int year = r.get(Calendar.YEAR);
   	int month = r.get(Calendar.MONTH);
   	int day = r.get(Calendar.DAY_OF_WEEK);*/
   	
   	//long alarmtime = t.convertDateIntegersToMillis(year, month, day, hour, min, sec);
   	//		   	String s = 	String.valueOf(year) + "-" + String.valueOf(month) + "-" + String.valueOf(day) + " " +
   	//		   				String.valueOf(hour) + ":" + String.valueOf(min) + ":" + String.valueOf(sec);
//	Log.d("Alarm Created from UI", t.convertMillisecondsToDateString(alarmtime));		
//	Log.d("Alarm Created icon:", String.valueOf(icon));		
//	Log.d("Alarm Created reps:", String.valueOf(rep));		
//	Log.d("Alarm Created enabled", String.valueOf(enabled));		
		


}